package org.trialiet.notedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.trialiet.notedemo.R;
import org.trialiet.notedemo.util.DBAdapter;

public class RegisterActivity extends Activity {
    private Button btnRegister;
    private String reUsername;
    private String rePassword;
    private EditText etUsername;
    private EditText etPassword;
    private DBAdapter dbAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbAdapter = new DBAdapter(this);
        btnRegister = (Button) this.findViewById(R.id.btn_register_register);
        etUsername = (EditText) this.findViewById(R.id.et_register_username);
        etPassword = (EditText) this.findViewById(R.id.et_register_password);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reUsername = etUsername.getText().toString();
                rePassword = etPassword.getText().toString();
                dbAdapter.open();
                if(dbAdapter.insertUser(reUsername, rePassword) > 0){
                    Toast.makeText(RegisterActivity.this, "Register Success", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.putExtra("username",reUsername);
                    intent.putExtra("password",rePassword);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                else{
                    Toast.makeText(RegisterActivity.this, "Existed Username", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        dbAdapter.close();
    }

}
